package com.kcs.fxmargin.action;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.action.form.PreSettlementActionForm;
import com.kcs.fxmargin.bo.PreSettlementBO;
import com.kcs.fxmargin.service.DealsService;

public class PreSettlementAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	private DealsService service = new DealsService();
	private String user = "";

	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		log.debug("PreSettlementAction : Start");

		try {

			PreSettlementActionForm formBean = (PreSettlementActionForm) form;
			
			user = formBean.getUserLoginName();

			if ("init".equals(formBean.getAction())) {
				formBean.resetExpect();
				formBean.setPreSettleBoList(new ArrayList<PreSettlementBO>());
			} else if ("search".equals(formBean.getAction())) {
				formBean.setPreSettleBoList(service.searchPreSettlement());
				formBean.setAction("init");
				request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
				
			} else if ("exportToPDF".equals(formBean.getAction())) {
				
				formBean.setAction("init");
				exportPreSettlementToPDF(formBean.getPreSettleBoList(), request, response);
			} else if ("exportToExcel".equals(formBean.getAction())) {
				
				formBean.setAction("init");
				exportPreSettlementToExcel(formBean.getPreSettleBoList(), request, response);
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}

		log.debug("PreSettlementAction : End");

		return mapping.findForward("preSettlement");
	}
	
	public void exportPreSettlementToPDF(List<PreSettlementBO> preSettleList, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] column 			= new String[]{"Type of Deal","Deal Id","Counterparty","Instrument","Branch Code","Comments","Days to Mat","Mat Date","Notional","Cur.","MarketValue_THB","Add On","Deduct Margin","Pre-settle Risk"};
		float[] columnWidth 		= new float[]{100f,100f,200f,250f,150f,100f,60f,150f,180f,60f,180f,180f,180f,180f};
		PdfPTable pdfTable 			= null;
		PdfPCell pdfCell 			= null;
		Document pdf 				= null;
		Paragraph paragraph 		= null;
		Font font 					= null;
		Font font_blod				= null;
		Font font_header			= null;
		BaseFont baseFont 			= null;
		String fontFile 			= null;
		ByteArrayOutputStream baos 	= null;
		OutputStream os	 			= null;
		Rectangle rect 				= null;
		boolean result 				= false;
		
		log.info("Begin : PreSettlementAction : exportPreSettlement");
		
		try{
			//Get Font Path
			
			// for test
//			fontFile = request.getServletContext().getRealPath("/WEB-INF/font/tahoma.ttf");
			
			// for deploy
			fontFile = "/app/fxm-jboss/jboss-as/server/fxm-jboss/log/font/ARIALUNI.TTF";
			
			log.info("fontFile : "+fontFile);
			
			//Create Pdf Document
			pdf = new Document();
			rect = PageSize.A4;
			rect = rect.rotate();
			pdf.setPageSize(rect);
			pdf.setMarginMirroring(true);
			pdf.setMargins(10, 10, 30, 30);
			baos = new ByteArrayOutputStream();
			PdfWriter.getInstance(pdf, baos);
			pdf.open();
			
			//Add meta data
			pdf.addTitle("Pre-Settlement Risk Detail  Report");
			pdf.addSubject("Pre-Settlement Risk Detail  Report");
			pdf.newPage();
			
			//Create Font
			font 		= new Font(baseFont, 7, Font.NORMAL);
			font_header = new Font(baseFont, 8, Font.NORMAL);
			font_blod	= new Font(baseFont, 7, Font.BOLD);
			
			//Add Header paragraph
			paragraph 		= new Paragraph();			
			PdfPTable table = new PdfPTable(3);
	        int widths[] 	= {100,100,100};
	        table.setWidths(widths);
	        table.setWidthPercentage(100);
	
	        PdfPCell c1 = new PdfPCell(new Phrase("", font));
	        c1.setHorizontalAlignment(Element.ALIGN_LEFT);
	        c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        c1.setBorder(0);
	        table.addCell(c1);
	
	        c1 = new PdfPCell(new Phrase("KRUNG THAI BANK PCL.\n\nPre-Settlement Risk Limit-Detail\n\n", font_header));
	        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	        c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        c1.setBorder(0);
	        table.addCell(c1);
	
	        c1 = new PdfPCell(new Phrase("Report Date : " + commonUtils.formatDate(new Date(), commonUtils.ddmmyyyyhhmmsswithslash) , font_blod));
	        c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
	        c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        c1.setBorder(0);
	        table.addCell(c1);
	        
	        paragraph.add(table);
	        pdf.add(paragraph);
			
			//Create Table
			pdfTable = new PdfPTable(columnWidth);
			pdfTable.setWidthPercentage(100);
			
			//Set Table Header Font
			baseFont = BaseFont.createFont(fontFile, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			font = new Font(baseFont,10,Font.BOLD);
			
			for(int i=0; i<column.length;i++){
				pdfCell = new PdfPCell(new Phrase(column[i],font_blod));
				pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				pdfTable.addCell(pdfCell);
			}
			
			pdfTable.setHeaderRows(1);
			
			//Set Table Data Font
			baseFont = BaseFont.createFont(fontFile, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			font = new Font(baseFont,8,Font.NORMAL);
			
			if(preSettleList != null){
				for(PreSettlementBO data : preSettleList){
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getTypeOfDeal()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getDealId()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getCounterParty()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_LEFT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getInstrument()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_LEFT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getBranceCode()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_LEFT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getComments()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_LEFT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getMaturity()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.formatDate(data.getMatdate(), commonUtils.ddmmyyyywithslash),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.parseToFormat(toStringNotNull(data.getNotional())),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(toStringNotNull(data.getCurr()),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.parseToFormat(toStringNotNull(data.getMktValue())),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.parseToFormat(toStringNotNull(data.getAddOn())),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.parseToFormat(toStringNotNull(data.getDeductMargin())),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					pdfTable.addCell(pdfCell);
					
					pdfCell = new PdfPCell(new Phrase(commonUtils.parseToFormat(toStringNotNull(data.getPreSettleRisk())),font));
					pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					pdfTable.addCell(pdfCell);
				}	
			}
			
			paragraph = new Paragraph();
			paragraph.add(pdfTable);
			
			pdf.add(paragraph);
			pdf.close();
			
			//Attatch file
			response.setHeader("Pragma", "no-cache");
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "attachment; filename=" + Constant.PDF_PRE_SETTLEMENT_FILENAME);
			
			os = response.getOutputStream();			
			baos.writeTo(os);
			
			// Create a reader
			PdfReader reader = new PdfReader(baos.toByteArray());
	        
			// Create a stamper
		    PdfStamper stamper= new PdfStamper(reader, os);

		    // Loop over the pages and add a header to each page
		    
		    int n = reader.getNumberOfPages();
		    for (int i = 1; i <= n; i++) {
		    	getHeaderTable(i, n, font).writeSelectedRows(0, -1, 10, 590, stamper.getOverContent(i));
		    	getFooterTable(user, font).writeSelectedRows(0, -1, 10, 20, stamper.getOverContent(i));
		    }
		        // Close the stamper
		    stamper.close();
		    reader.close();
			
			os.flush();
			os.close();
			
			result = true;
			
		}catch(Exception e){
			if(pdf != null){
				pdf.close();
			}
			throw e;
		}
		
		log.info("export result: " + result);
		log.info("End : PreSettlementAction : exportPreSettlement");
	}
	
	/**
     * Create a header table with page X of Y
     * @param x the page number
     * @param y the total number of pages
     * @return a table that can be used as header
     */
    public static PdfPTable getHeaderTable(int x, int y, Font font) {
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(827);
        table.setWidthPercentage(100);
        table.getDefaultCell().setFixedHeight(20);
        table.getDefaultCell().setBorder(0);
        table.addCell("");
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(new Phrase(String.format("Page %d of %d", x, y), font));
        return table;
    }
    
    public static PdfPTable getFooterTable(String user, Font font) {
    	PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(827);
        table.setWidthPercentage(100);
           
        PdfPCell c1 = new PdfPCell(new Phrase("Print By : " + user, font));
        c1.setHorizontalAlignment(Element.ALIGN_LEFT);
        c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        c1.setBorder(0);        
        table.addCell(c1);
           
        PdfPCell c2 = new PdfPCell(new Phrase(commonUtils.formatDate(new Date(), commonUtils.ddmmyyyyhhmmsswithslash), font));
        c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        c2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        c2.setBorder(0);
        table.addCell(c2);
        
        return table;
    }
	
	private String toStringNotNull(Object o)throws Exception{
		return (o==null)?"":o.toString();
	}
	
	@SuppressWarnings("deprecation")
	public void exportPreSettlementToExcel(List<PreSettlementBO> preSettleList, HttpServletRequest request, HttpServletResponse response) throws Exception{
		log.debug("PreSettlementAction : exportPreSettlementToExcel : Start");
		
		ServletOutputStream out	= null;
		HSSFWorkbook workbook 	= null;
		HSSFSheet sheet 		= null;
		HSSFRow row 			= null;
		HSSFCell cell 			= null;
		PreSettlementBO bo 		= null;
		HSSFCellStyle style 	= null;
		HSSFFont font 			= null;
		Integer index			= 0;
		
		if(preSettleList != null && preSettleList.size() > 0){
			workbook 	= new HSSFWorkbook();
			sheet 		= workbook.createSheet("Pre Settlement Report");
			style 		= workbook.createCellStyle();
			font 		= workbook.createFont();
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
						
			row = sheet.createRow(index);
			style = workbook.createCellStyle();
			style.setWrapText(false);
			style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			cell = row.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Report Date : " + commonUtils.formatDate(new Date(), commonUtils.ddmmyyyyhhmmsswithslash));
			cell.setCellStyle(style);
			index++;
			
			row = sheet.createRow(index);
			style = workbook.createCellStyle();
			style.setWrapText(false);
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setFont(font);
			cell = row.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue("KRUNG THAI BANK PLC.");
			cell.setCellStyle(style);
			index++;
			
			row = sheet.createRow(index);
			style = workbook.createCellStyle();
			style.setWrapText(false);
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setFont(font);
			cell = row.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Pre-Settlement Risk Limit - Detail");
			cell.setCellStyle(style);
			index++;
			
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 13));
			sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 13));
			sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 13));
			index++;
			
			//-- Header cell style
			style = workbook.createCellStyle();
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setFont(font);
			style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			style.setFillForegroundColor(new HSSFColor.AQUA().getIndex());
		
			row = sheet.createRow(index);
			cell = row.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Type of Deal");
			cell.setCellStyle(style);
			cell = row.createCell(1, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Deal Id");
			cell.setCellStyle(style);
			cell = row.createCell(2, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Counterparty");
			cell.setCellStyle(style);
			cell = row.createCell(3, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Instrument");
			cell.setCellStyle(style);
			cell = row.createCell(4, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Branch Code");
			cell.setCellStyle(style);
			cell = row.createCell(5, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Comments");
			cell.setCellStyle(style);
			cell = row.createCell(6, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Days to Mat");
			cell.setCellStyle(style);
			cell = row.createCell(7, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Mat Date");
			cell.setCellStyle(style);
			cell = row.createCell(8, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Notional");
			cell.setCellStyle(style);
			cell = row.createCell(9, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Cur.");
			cell.setCellStyle(style);
			cell = row.createCell(10, Cell.CELL_TYPE_STRING);
			cell.setCellValue("MarketValue_THB");
			cell.setCellStyle(style);
			cell = row.createCell(11, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Add On");
			cell.setCellStyle(style);
			cell = row.createCell(12, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Deduct Margin");
			cell.setCellStyle(style);
			cell = row.createCell(13, Cell.CELL_TYPE_STRING);
			cell.setCellValue("Pre-settle Risk");
			cell.setCellStyle(style);
			index++;
					
			for(int i = 0 ; i < preSettleList.size() ; i++){
				bo = preSettleList.get(i);
				
				row = sheet.createRow(i+index);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				cell = row.createCell(0, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getTypeOfDeal());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				cell = row.createCell(1, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getDealId());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
				cell = row.createCell(2, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getCounterParty());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
				cell = row.createCell(3, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getInstrument());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
				cell = row.createCell(4, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getBranceCode());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
				cell = row.createCell(5, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getComments());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				cell = row.createCell(6, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getMaturity());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				cell = row.createCell(7, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.formatDate(bo.getMatdate(), commonUtils.ddmmyyyywithslash));
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
				cell = row.createCell(8, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.parseToFormat(bo.getNotional() + ""));
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				cell = row.createCell(9, Cell.CELL_TYPE_STRING);
				cell.setCellValue(bo.getCurr());
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
				cell = row.createCell(10, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.parseToFormat(bo.getMktValue() + ""));
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
				cell = row.createCell(11, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.parseToFormat(bo.getAddOn() + ""));
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
				cell = row.createCell(12, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.parseToFormat(bo.getDeductMargin() + ""));
				cell.setCellStyle(style);
				
				style = workbook.createCellStyle();
				style.setWrapText(false);
				style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
				cell = row.createCell(13, Cell.CELL_TYPE_STRING);
				cell.setCellValue(commonUtils.parseToFormat(bo.getPreSettleRisk() + ""));
				cell.setCellStyle(style);

			}
			
			HSSFFooter footer= sheet.getFooter();
			footer.setLeft("Print By : " + user);
			footer.setRight(commonUtils.formatDate(new Date(), commonUtils.ddmmyyyyhhmmsswithslash));
			
			@SuppressWarnings("unchecked")
			java.util.Iterator<HSSFCell> it = row.cellIterator(); 
		    while(it.hasNext()){ 
			         HSSFCell cellTmp = it.next(); 
			         sheet.autoSizeColumn((short)cellTmp.getCellNum(),true); 
			 }
			
		    sheet.getPrintSetup().setLandscape(true);
		    sheet.getPrintSetup().setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		    
			response.setHeader("Pragma", "no-cache");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=" + Constant.EXCEL_PRE_SETTLEMENT_RISK_DETAIL_FILENAME);
			
			out = response.getOutputStream();
			workbook.write(out);
			//out.flush();
			out.close();
			request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
			
			log.debug("Export excel successfully.");			

			log.debug("PreSettlementAction : exportPreSettlementToExcel : End");
		}else{
			throw new Exception("Cannot export Excel: Data error.");
		}
	}
}
